/**
 Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;







import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.BResult;
import com.thinkgem.jeesite.modules.ta.service.BResultService;
import com.thinkgem.jeesite.modules.ta.util.WebServiceUtil;


/**
 * Controller
 * 
 * @author 刘雨轩
 * @version 2017-06-07
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/BResult")
public class BResultController extends BaseController {

	@Autowired
	private BResultService bResultService;
	
	@RequiresPermissions("ta:BResult:view")
	@RequestMapping(value = {"list",""})
	public String find(String startDate,String endDate,String priority,String name,String wname,String isComplete,String notComplete,String isCommand,String notCommand,String isStop,String notStop,String iid ,HttpServletRequest request,
			HttpServletResponse response, Model model) throws UnsupportedEncodingException {
		wname = ascii2native(wname);
		name = ascii2native(name);
		if(isComplete == null){
			isComplete = "on";
		}
		if(notComplete == null){
			notComplete = "on";
		}
		if(isCommand == null){
			isCommand = "on";
		}
		if(notCommand == null){
			notCommand = "on";
		}
		if(isStop == null){
			isStop = "on";
		}
		if(notStop == null){
			notStop = "on";
		}
		User user = UserUtils.getUser();
		if(user == null){
			return "modules/sys/sysLogin";
		}
		String userId = user.getNo();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("wname", wname);
		map.put("priority", priority);
		map.put("name", name);
		map.put("isComplete", isComplete);
		map.put("notComplete", notComplete);
		map.put("isCommand", isCommand);
		map.put("notCommand", notCommand);
		map.put("isStop", isStop);
		map.put("notStop", notStop);
		map.put("iid", iid);
		
		List<BResult> ls = bResultService.find(map);
		model.addAttribute("ls", ls);
		return "modules/ta/BResultList";
	}
	
	public String ascii2native(String ascii) {  
	    int n = ascii.length() / 6;  
	    StringBuilder sb = new StringBuilder(n);  
	    for (int i = 0, j = 2; i < n; i++, j += 6) {  
	        String code = ascii.substring(j, j + 4);  
	        char ch = (char) Integer.parseInt(code, 16);  
	        sb.append(ch);  
	    }  
	    return sb.toString();  
	}  
	
	@RequiresPermissions("ta:BResult:view")
	@RequestMapping(value = {"ins"})
	public String ins(String iid,HttpServletRequest request) {
		WebServiceUtil.ins(iid, request);
		return "redirect:"+Global.getAdminPath()+"/ta/BResult/?repage";
	}
	

}